#!/bin/sh

die()
{
    echo $1
    exit 1
}


s_install()
{
    src_DIR=$1
    mode=$2
    dst_DIR=$RPM_BUILD_DIR/`echo $3 | sed 's/\///'`	

    [ -d $dst_DIR ] || mkdir -p $dst_DIR

    THISDIR=`pwd`
    cd $src_DIR
    for f in `ls -1`
    do
        if [ -f $f ]
	then
	   install -m $mode $f $dst_DIR/$f
	fi
    done
    cd $THISDIR
}


print_attr()
{
    dst_DIR=$1
    mode=$2
    t_dir=$RPM_BUILD_DIR/$dst_DIR

    THISDIR=`pwd`
    cd $t_dir
    for f in `ls -1`
    do
        [ -f $f ] && echo "%attr($mode, root, root) $dst_DIR/$f"
    done
    cd $THISDIR
}

make_targz()
{
    THISDIR=`pwd`
    src_dir=$1
    cd $RPM_BUILD_DIR/..
    tar cvf - $src_dir |\
	gzip > /usr/src/redhat/SOURCES/$src_dir.tar.gz
    cd $THISDIR
}

make_spec()
{
    name=$1
    cat <<-LEOF > $TOPDIR/dist/RPM/$name.spec

Summary: A C++ scene graph API on OpenGL for real time graphics
Name: $name
Version: $REV
Release: $REL
Copyright: LGPL
Group: Graphics
Source: $name-$REV.tar.gz
URL: http://www.openscenegraph.org
Packager: `awk -f - /etc/passwd <<-EOF
BEGIN { FS=":"}
{
    if( \\$1 == "$LOGNAME" )
    print \\$5
}
EOF`

%description

The Open Scene Graph is a cross-platform C++/OpenGL library for the real-time 
visualization. Uses range from visual simulation, scientific modeling, virtual 
reality through to games.  Open Scene Graph employs good practices in software
engineering through the use of standard C++, STL and generic programming, and
design patterns.  Open Scene Graph strives for high performance and quality in
graphics rendering, protability, and extensibility

%prep

%setup

%build

%install

cd $RPM_BUILD_DIR 
tar cvf - . | tar xvfC - /

# ---------------------
# FILES Sections
%files

LEOF
}


make_osg()
{
    RPM_BUILD_DIR=/usr/src/redhat/BUILD/OpenSceneGraph-$REV
    rm -rf $RPM_BUILD_DIR
    mkdir $RPM_BUILD_DIR

    s_install $TOPDIR/lib 755 $INST_LIBS
    s_install $TOPDIR/lib/osgPlugins 755 $INST_PLUGINS
    s_install $TOPDIR/bin 755 $INST_EXAMPLES

    make_targz OpenSceneGraph-$REV
    make_spec OpenSceneGraph

    print_attr $INST_LIBS 755    >> $TOPDIR/dist/RPM/OpenSceneGraph.spec
    print_attr $INST_PLUGINS 755 >> $TOPDIR/dist/RPM/OpenSceneGraph.spec
    print_attr $INST_EXAMPLES 755   >> $TOPDIR/dist/RPM/OpenSceneGraph.spec

    rpmbuild -bb --clean $TOPDIR/dist/RPM/OpenSceneGraph.spec

    mv /usr/src/redhat/RPMS/i386/OpenSceneGraph-$REV-$REL.i386.rpm dist/RPM
}

make_osg_dev()
{
    RPM_BUILD_DIR=/usr/src/redhat/BUILD/OpenSceneGraph_dev-$REV
    rm -rf $RPM_BUILD_DIR
    mkdir $RPM_BUILD_DIR

    for d in `ls $TOPDIR/include | grep -v CVS`
    do
        [ -d $TOPDIR/include/$d ] && \
            s_install $TOPDIR/include/$d 644 $INST_INCLUDE/$d
    done


    for d in `ls -1 $INST_EXAMPLE_SRC`
    do
        if [ -d $INST_EXAMPLE_SRC/$d ] 
        then
           s_install $INST_EXAMPLE_SRC/$d 644 $INST_EXAMPLE_SRC/$d
        fi
        cd $THISDIR
    done

    s_install $INST_SRC/Make 644 $INST_SRC/Make


    make_targz OpenSceneGraph_dev-$REV
    make_spec OpenSceneGraph_dev

    for d in `ls -1 $RPM_BUILD_DIR/$INST_INCLUDE`
    do
        print_attr $INST_INCLUDE/$d 755  >> $TOPDIR/dist/RPM/OpenSceneGraph_dev.spec
    done

    for d in `ls -1 $RPM_BUILD_DIR/$INST_EXAMPLE_SRC`
    do
        print_attr $INST_EXAMPLE_SRC/$d 755  >> $TOPDIR/dist/RPM/OpenSceneGraph_dev.spec
    done

    print_attr $INST_SRC/Make 755 >>  $TOPDIR/dist/RPM/OpenSceneGraph_dev.spec

    rpm -bb --clean $TOPDIR/dist/RPM/OpenSceneGraph_dev.spec

    mv /usr/src/redhat/RPMS/i386/OpenSceneGraph_dev-$REV-$REL.i386.rpm dist/RPM
}

echo "The current $0 script is broken and needs to be updated."
exit 1

if [ "`whoami`" != "root" ] 
then
    echo
    echo  "    === makerpm : You must be root do make the RPM distribution."
    echo
    exit 1
fi

if [ $# -ne 10 ] 
then
    echo
    echo makerpm : Internal Error - Bad Usage
    echo 
    exit 1
fi

OS=$1
TOPDIR=$2
INST_LIBS=$3
INST_PLUGINS=$4
INST_INCLUDE=$5
INST_EXAMPLES=$6
INST_SRC=$7
INST_EXAMPLE_SRC=$8
shift;shift; shift;shift; shift;shift; shift;shift;
INST_DOC=$1
INST_DATA=$2

REV="`make -s version`"
[ -z "$REV" ] && die  "makerpm: requires a functional $TOPDIR/bin/osgversion"

REL=1


make_osg

make_osg_dev



exit 0
